package com.aimao.blog.service.impl;

import com.aimao.blog.constant.ServerResponse;
import com.aimao.blog.constant.TableConst;
import com.aimao.blog.dao.ArticleMapper;
import com.aimao.blog.dao.CommentMapper;
import com.aimao.blog.dao.SortMapper;
import com.aimao.blog.dao.TagMapper;
import com.aimao.blog.pojo.Article;
import com.aimao.blog.service.ArticleAdminService;
import com.aimao.blog.vo.ArticleAndCommentCountVo;
import com.aimao.blog.vo.ArticleAndTagSortVo;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author PuaChen
 * @Create 2018-07-03 13:56
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class ArticleAdminServiceImpl implements ArticleAdminService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SortMapper sortMapper;

    @Autowired
    private TagMapper tagMapper;

    @Override
    public ServerResponse<Article> pushArticle(Article article) {
        if (article == null) {
            return ServerResponse.createFailure("参数错误");
        }
        article.setId(null);
        if (article.getRootsort() == null) {
            article.setRootsort(1);
        }
        article.setCreatetime(new Date());
        article.setUpdatetime(new Date());
        int row = articleMapper.insertSelective(article);
        if (row <= 0) {
            return ServerResponse.createFailure("发布失败");
        }
        return ServerResponse.createSuccessAndData(article);
    }


    @Override
    public ServerResponse<Article> updateArticle(Article article) {
        if (article == null || article.getId() == null) {
            return ServerResponse.createFailure("参数错误");
        }
        article.setUpdatetime(new Date());
        int row = articleMapper.updateByPrimaryKeySelective(article);
        if (row <= 0) {
            return ServerResponse.createFailure("更新失败");
        }
        return ServerResponse.createSuccessAndData(article);
    }

    @Override
    public ServerResponse deleteArticle(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return ServerResponse.createFailure("参数错误");
        }
        int row = articleMapper.deleteByPrimaryKeyAsList(ids);
        if (row <= 0) {
            return ServerResponse.createFailure("删除失败");
        }
        return ServerResponse.createSuccessAndData(row);
    }

    @Override
    public ServerResponse<IPage<ArticleAndCommentCountVo>> findALl(boolean isSplit, Integer page, Integer size) {
        if (isSplit) {
            if (page == null || size == null) {
                return ServerResponse.createFailure("参数错误");
            }
        }
        IPage<ArticleAndCommentCountVo> data = articleMapper.selectAll(new Page(page == null ? 1 : page, size == null ? -100 : size));
        return ServerResponse.createSuccessAndData(data);
    }

    @Override
    public ServerResponse<IPage<ArticleAndCommentCountVo>> findAllBySortId(Integer sortId, boolean isSplit, Integer page, Integer size) {
        if (sortId == null) {
            return ServerResponse.createFailure("参数错误");
        }
        if (isSplit) {
            if (page == null || size == null) {
                return ServerResponse.createFailure("参数错误");
            }
        }
        return ServerResponse.createSuccessAndData(articleMapper.selectAllBySort(new Page(page == null ? 1 : page, size == null ? -100 : size), sortId));
    }

    @Override
    public ServerResponse<ArticleAndCommentCountVo> findToId(Integer id) {
        if (id == null) {
            return ServerResponse.createFailure("参数错误");
        }
        return ServerResponse.createSuccessAndData(articleMapper.selectById(id));
    }

    @Override
    public ServerResponse<IPage<ArticleAndCommentCountVo>> findToLike(String keyWord, boolean isSplit, Integer page, Integer size) {
        if (StringUtils.isBlank(keyWord)) {
            return ServerResponse.createFailure("请提供关键字");
        }
        if (isSplit) {
            if (page == null || size == null) {
                return ServerResponse.createFailure("参数错误");
            }
        }
        keyWord = "%" + keyWord + "%";
        return ServerResponse.createSuccessAndData(articleMapper.selectToLike(new Page(page == null ? 1 : page, size == null ? -100 : size), keyWord));
    }

    @Override
    public ServerResponse<IPage<Article>> findDraftToAll(boolean isSplit, Integer page, Integer size) {
        if (isSplit) {
            if (page == null || size == null) {
                return ServerResponse.createFailure("参数错误");
            }
        }
        return ServerResponse.createSuccessAndData(articleMapper.selectAllByType(new Page(page == null ? 1 : page, size == null ? -100 : size), TableConst.ArticleConst.DRAFT));
    }

    @Override
    public ServerResponse<IPage<ArticleAndCommentCountVo>> findToIdList(List<Integer> ids, boolean isSplit, Integer page, Integer size) {
        if (ids == null) {
            return ServerResponse.createFailure("参数错误");
        }
        if (isSplit) {
            if (page == null || size == null) {
                return ServerResponse.createFailure("参数错误");
            }
        }
        return ServerResponse.createSuccessAndData(articleMapper.selectByPrimaryKeyAsList(new Page(page == null ? 1 : page, size == null ? -100 : size), ids));
    }

    @Override
    public ServerResponse cancelTopAll() {
        articleMapper.cancelAllTop();
        return ServerResponse.createSuccess();
    }

    @Override
    public ServerResponse setTop(Integer id) {
        if (id == null) {
            return ServerResponse.createFailure("参数错误");
        }
        cancelTopAll();
        Article article = new Article();
        article.setId(id);
        article.setTop(true);
        int row = articleMapper.updateByPrimaryKeySelective(article);
        if (row <= 0) {
            return ServerResponse.createFailure("置顶失败");
        }
        return ServerResponse.createSuccess();
    }

    @Override
    public ServerResponse pushDraftToNormal(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return ServerResponse.createFailure("参数错误");
        }
        return ServerResponse.createSuccessAndData(articleMapper.updatePushDraftToNormal(ids));
    }

    @Override
    public ServerResponse<ArticleAndCommentCountVo> getArticleTop() {
        return ServerResponse.createSuccessAndData(articleMapper.getArticleTopBean());
    }

    @Override
    public ServerResponse<ArticleAndTagSortVo> getArticleDetail(Integer id) {
        Article article = articleMapper.selectByPrimaryKey(id);
        if (article == null) {
            return ServerResponse.createFailure("获取失败");
        }
        int commentCount = commentMapper.selectArticleCommentCount(article.getId());
        ArticleAndTagSortVo articleAndTagSortVo = new ArticleAndTagSortVo();
        if (StringUtils.isNotBlank(article.getTag())) {
            List<Integer> tagIds = JSONArray.parseArray(article.getTag(), Integer.class);
            if (tagIds != null && !tagIds.isEmpty()) {
                articleAndTagSortVo.setTags(tagMapper.selectAllByIds(tagIds));
            }
        }
        articleAndTagSortVo.setSort(sortMapper.selectByPrimaryKey(article.getRootsort()));
        articleAndTagSortVo.setCommentCount(Long.valueOf(commentCount));
        articleAndTagSortVo.setArticle(article);
        return ServerResponse.createSuccessAndData(articleAndTagSortVo);
    }

    @Override
    public ServerResponse updateArticleReadCount(Integer id) {
        if (id == null) {
            return ServerResponse.createFailure("参数错误");
        }
        int row = articleMapper.updateReadCount(id);
        if (row <= 0) {
            return ServerResponse.createFailure();
        }
        return ServerResponse.createSuccess();
    }

    @Override
    public ServerResponse updateCancelTopAll() {
        int row = articleMapper.cancelAllTop();
        if (row <= 0) {
            return ServerResponse.createFailure();
        }
        return ServerResponse.createSuccess();
    }
}
